package com.efast.cafe.portal.service.doc.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.bean.EldfTreeBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.DocConstant;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.dao.doc.DocFolderDao;
import com.efast.cafe.portal.entity.doc.DocFolder;
import com.efast.cafe.portal.service.doc.IDocFolderService;
import com.efast.cafe.portal.service.doc.IDocUserService;
@Service
@Transactional
public class DocFolderServiceImpl implements IDocFolderService {

	private DocFolderDao docFolderDao;
	@Autowired
	private IDocUserService docUserService;
	
	@Autowired
	public void setDocFolderDao(DocFolderDao docFolderDao) {
		this.docFolderDao = docFolderDao;
	}

	@Override
	public List<EldfTreeBean> queryCompanyFolderTree(Integer pid, Integer asyn, String type) {
		return docFolderDao.queryCompanyFolderTree(SpringWebUtils.getRequestCompany(), pid, asyn, type);
	}

	@Override
	public List<EldfTreeBean> queryMyFolderTree(Integer pid, Integer asyn) {
		return docFolderDao.queryMyFolderTree(SpringWebUtils.getRequestCompany(), SpringWebUtils.getRequestUser(), pid, asyn);
	}

	@Override
	public void addCompanyFolder(DocFolder folder) {
		String companyCode = SpringWebUtils.getRequestCompany();
		folder.setCreateUser(SpringWebUtils.getRequestUser());
		folder.setCompanyCode(companyCode);
		if(folder.getGroupId()==null){
			DocFolder parentFolder = docFolderDao.getById(companyCode, folder.getParentId());
			folder.setGroupId(parentFolder.getGroupId());
		}
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(checkFolderPur(folder.getGroupId(), DocConstant.DocPurviews.PURCODE_CREATE_FOLDER)==0){
			throw new ServiceException("PURCODE_CREATE_FOLDER_ERROR");//没有创建企业文件夹的权限
		}
		}
		docFolderDao.addCompanyFolder(folder);
	}

	@Override
	public void updateCompanyFolder(DocFolder folder) {
		String companyCode = SpringWebUtils.getRequestCompany();
		folder.setCompanyCode(companyCode);
		folder.setLastUpdateUser(SpringWebUtils.getRequestUser());
		if(folder.getGroupId()==null){
			DocFolder folderDetail = docFolderDao.getById(companyCode, folder.getId());
			folder.setGroupId(folderDetail.getGroupId());
		}
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(checkFolderPur(folder.getGroupId(), DocConstant.DocPurviews.PURCODE_UPDATE_FOLDER)==0){
			throw new ServiceException("PURCODE_UPDATE_FOLDER_ERROR");//没有修改企业文件夹的权限
		}}
		docFolderDao.updateCompanyFolder(folder);
	}

	@Override
	public void addMyFolder(DocFolder folder) {
		String companyCode = SpringWebUtils.getRequestCompany();
		folder.setGroupId(0);
		folder.setCreateUser(SpringWebUtils.getRequestUser());
		folder.setCompanyCode(companyCode);
		folder.setUsername(SpringWebUtils.getRequestUser());
		docFolderDao.addMyFolder(folder);
	}

	@Override
	public void updateMyFolder(DocFolder folder) {
		folder.setCompanyCode(SpringWebUtils.getRequestCompany());
		folder.setLastUpdateUser(SpringWebUtils.getRequestUser());
		folder.setUsername(SpringWebUtils.getRequestUser());
		docFolderDao.updateMyFolder(folder);
	}

	@Override
	public int checkFolderPur(Integer groupId, String purCode) {
		return docFolderDao.checkFolderPur(SpringWebUtils.getRequestCompany(), SpringWebUtils.getRequestUser(), purCode, groupId);
	}

	@Override
	public DocFolder getById(Integer id) {
		return docFolderDao.getById(SpringWebUtils.getRequestCompany(), id);
	}

}
